# 确定性收敛示意图
# f(x) = 1/x
rm(list = ls())
library(ggplot2)
n <- -(1:150)
x <- 1/n
picdata <- data.frame(n = n, x = x)
ggplot(aes(x = x, y = 0), data = picdata) + geom_point(size = 3, shape = 1) +
  geom_vline(xintercept = 0) + geom_hline(yintercept = 0) +
  theme_bw()
# ggsave('data-raw/deter.pdf')

# 依概率收敛收敛: 样本均值分布的依概率收敛
picdata <- NULL
for (n in c(1,5,10)){
  ans <- data.frame(x = seq(-3,3,0.01),
                        y = dnorm(seq(-3,3,0.01), mean = 0, sd = 1/n),
                        size = n)
  picdata <- rbind(picdata, ans)
}
picdata$size <- as.factor(picdata$size)
ggplot(aes(x = x, y = y, color = size), data = picdata) + geom_line() +
  geom_vline(xintercept = 0) +
  theme_bw()
# ggsave('data-raw/plim.pdf')

# 依分布收敛
picdata <- data.frame(y1 = dt(x = seq(-3,3,0.1),df = 1),
           y2 = dt(x = seq(-3,3,0.1),df = 4),
           y3 = dt(x = seq(-3,3,0.1),df = 8),
           y4 = dnorm(seq(-3,3,0.1)))
picdata$x <- seq(-3,3,0.1)
ggplot(aes(x = x, y = y1), data = picdata) + geom_line() +
  geom_line(aes(y = y2), color = 'red') +
  geom_line(aes(y = y3), color = 'blue') +
  geom_line(aes(y = y4), size = 2) +
  geom_vline(xintercept = 0) + geom_hline(yintercept = 0) +
  geom_text(aes(x = -2.5, y = 0.35), label = '粗线是正态分布\n 形状越来越相似') +
  theme_bw()
# ggsave('data-raw/dlim.png')
